const { toast } = require("../../../utils/Tools")
const { sendChangeData } = require("../../../network/other")
// pages/mine/mine-changedata/mine-changedata.js
const app = getApp()
Page({
  data: {
    userinfo: null,
    pics:[]
  },
  onLoad: function (options) {
    const userinfo = wx.getStorageSync('business_user')
    if(userinfo){
      this.setData({
        userinfo
      })
    }
  },
  handleChangeLogo(){   //  点击改变LOGO
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        this.data.pics = res.tempFilePaths;
        this.setData({
          pics: this.data.pics,
          [`userinfo.face_url`]:this.data.pics[0]
        })
      }
    })
  },
  handleInput(e){ ///  处理输入框输入
    const {type} = e.currentTarget.dataset
    this.setData({
      [`userinfo.${type}`]: e.detail.value
    })
  },
  sendSuccess(){
    toast('上传成功','success')
    const savedata = Object.assign(wx.getStorageSync('business_user'),{
      name:this.data.userinfo.name,
      telephone:this.data.userinfo.telephone,
      face_url:this.data.pics[0] || this.data.userinfo.face_url
    })
    wx.setStorageSync('business_user', savedata);
    setTimeout(()=>{
      wx.reLaunch({
        url: '/pages/achievement/achievement/achievement'
      });
    },1500)
  },
  handleSubmit(){
    wx.showModal({
      title: `操作提醒`,
      content: `是否提交修改`,
      success: res => {
        if(this.data.pics.length>0){
          let data = {}
          if(this.data.userinfo.psw){
            data = {
              name: this.data.userinfo.name,
              telephone: this.data.userinfo.telephone,
              password:this.data.userinfo.psw,
              token:wx.getStorageSync('token')
            }
          }else{
            data = {
              name: this.data.userinfo.name,
              telephone: this.data.userinfo.telephone,
              token:wx.getStorageSync('token')
            }
          }
          wx.uploadFile({
            url: app.globalData.BASE_URL+'api/servicer/servicer_edite',
            filePath: this.data.pics[0],
            name: `face_url`,
            formData: data, 
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: result => {
              const res = JSON.parse(result.data)
              if(res.code === 200){
                this.sendSuccess()
              }else{
                toast(`上传失败,失败信息:${res.msg}`)
              }
            },
            fail: err =>{
              console.warn(err)
            }
          })
        }else{
          let data = {}
          if(this.data.userinfo.psw){
            data = {
              name: this.data.userinfo.name,
              telephone: this.data.userinfo.telephone,
              password:this.data.userinfo.psw
            }
          }else{
            data = {
              name: this.data.userinfo.name,
              telephone: this.data.userinfo.telephone
            }
          }
          sendChangeData(data).then(res=>{
            if(res.code === 200){
              this.sendSuccess()
            }else{
              toast(`上传失败,失败信息:${res.msg}`)
            }
          }).catch(err=>{
            console.warn(err)
          })
        }
      }
    });
  }
})